package com.blackline.auto.Task;

import static org.junit.Assert.fail;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.concurrent.TimeUnit;

import org.apache.log4j.Logger;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.firefox.FirefoxProfile;

import com.blackline.auto.common.CommonMethods;
import com.blackline.declareUI.UI;
import com.blackline.parameters.Parameters;
import com.blackline.parameters.TestData;

/**
 * @author XBO
 * @version Feb, 2013
 * <p>The smoke test to Task module.
 */
public class Task_smoketest {
	
	private WebDriver driver;
	private StringBuffer verificationErrors = new StringBuffer();
	private static Logger logger=Logger.getLogger(Task_smoketest.class);
	UI UI=new UI();
	Parameters parameters;
	@Before
	public void setUp() throws Exception {
		FirefoxProfile profile=new FirefoxProfile();
		profile.setEnableNativeEvents(true);
		parameters=new Parameters("parameters.xml");

		driver = new FirefoxDriver();
		driver.manage().window().maximize();
		driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
		UI.declareAllUI(driver);
	}
	
	@Test
	public void Tasksmoketest() throws Exception {
	
	//Data preparation
		TestData testdata=new TestData("TestData/Task_smoketest.xml");
		StringBuffer verificationErrors = new StringBuffer();
		DateFormat df = new SimpleDateFormat("yyyyMMdd-HH-mm");  
	    String timeString = df.format(new Date());
	    logger.info("The timeString is: " + timeString);
	    int sleepTime1 = 2000;
	//========================
	    
    //Add new task
		UI.LogonPage.Login(driver, parameters.urlDev2, parameters.getLogonID("admin"), parameters.getPassword("admin"));
		
		UI.NavigationMenu.TASKS.click();
		UI.NavigationMenu.Tasks.click();
		UI.Tasks.Add_New_Task_link.click();

		UI.AssignmentPage_Tasks.Description_textField.sendKeys(timeString);
		UI.AssignmentPage_Tasks.Start_Date_textField.sendKeys(testdata.getInput("startDate"));
		UI.AssignmentPage_Tasks.Due_Date_textField.sendKeys(testdata.getInput("dueDate"));
		UI.AssignmentPage_Tasks.Preparer_textField.sendKeys(parameters.getFullName("preparer"));
		Thread.sleep(sleepTime1);
		UI.AssignmentPage_Tasks.Copyright_text.click();
		UI.AssignmentPage_Tasks.Approver_textField.sendKeys(parameters.getFullName("approver"));
		Thread.sleep(sleepTime1);
		UI.AssignmentPage_Tasks.Copyright_text.click();
		UI.AssignmentPage_Tasks.Reviewer_textField.sendKeys(parameters.getFullName("reviewer"));
		Thread.sleep(sleepTime1);
		UI.AssignmentPage_Tasks.Copyright_text.click();
		UI.AssignmentPage_Tasks.Account_Reviewer_textField.sendKeys(parameters.getFullName("ar"));
		Thread.sleep(sleepTime1);
		UI.AssignmentPage_Tasks.Copyright_text.click();
		UI.AssignmentPage_Tasks.Account_Manager_textField.sendKeys(parameters.getFullName("am"));
		Thread.sleep(sleepTime1);
		UI.AssignmentPage_Tasks.Copyright_text.click();
		UI.AssignmentPage_Tasks.Internal_Auditor_textField.sendKeys(parameters.getFullName("ia"));
		Thread.sleep(sleepTime1);
		UI.AssignmentPage_Tasks.Copyright_text.click();
		Thread.sleep(3000);//Don't remove
		
		UI.AssignmentPage_Tasks.Save_button.click();
		
		//Verify the task can be listed in "Task" table.
		UI.Tasks.Description_textField.sendKeys(timeString);
		UI.Tasks.Start_textField.sendKeys(testdata.getInput("startDate"));
		UI.Tasks.Due_Days_textField.sendKeys(testdata.getInput("dueDate"));
		UI.Tasks.Apply_link.click();
		if ("1".equals(UI.Tasks.first_number_inResultText.getText()))
		{
			logger.info("[Pass!] The newly created task exists in the list.");
		}
		else
		{
			logger.error("[Fail!] The newly created task does NOT exist in the list.");
			verificationErrors.append("[Fail!] The newly created task does NOT exist in the list.");
			
		}
		UI.SessionMenu.LogOut.click();
		logger.info("Has been logged out.");
		
	
	//Cerrify by preparer
		//Login as preparer
		UI.LogonPage.Login(driver, parameters.urlDev2, parameters.getLogonID("preparer"), parameters.getPassword("preparer"));
		
		//Verify role "Preparer" text exists
		if ("PREPARER".equals(UI.SessionMenu.Role.getText()))
				{
					logger.info("[Pass!] The text of the role is: PREPARER");
				}
		else
		{
			logger.error("[Fail!] The text of the role is NOT: PREPARER");
			verificationErrors.append("[Fail!] The text of the role is NOT: PREPARER");
		}
		UI.NavigationMenu.TASKS.click();
		UI.Tasks.Description_textField.sendKeys(timeString);
		UI.Tasks.Apply_link.click();
		if ("1".equals(UI.Tasks.first_number_inResultText.getText()))
		{
			logger.info("[Pass!] The newly created task exists in the list.");
		}
		else
		{
			logger.error("[Fail!] The newly created task does NOT exist in the list.");
			verificationErrors.append("[Fail!] The newly created task does NOT exist in the list.");
		}
		
		
		UI.Tasks.Edit_iconOf1stResearchedResult.click();
		UI.Task.Certify_button.click();
		
		//Verify the status link's text is 'View'
		if (!"".equals(UI.Tasks.Status_linkOf1stResearchedResult.getText()))
		{
			logger.info("[Pass!] The current status is: View");
		}
		else
		{
			logger.info("[Fail!] The current status is NOT: View");
			verificationErrors.append("[Fail!] The current status is NOT: View");
		}
		UI.SessionMenu.LogOut.click();
		
	//Reject by approver
		//Login as Approver
		UI.LogonPage.Login(driver, parameters.urlDev2, parameters.getLogonID("approver"), parameters.getPassword("approver"));

		//Verify role "APPROVER" text exists
		if ("APPROVER".equals(UI.SessionMenu.Role.getText()))
				{
					logger.info("[Pass!] The text of the role is: APPROVER");
				}
		else
		{
			logger.error("[Fail!] The text of the role is NOT: APPROVER");
			verificationErrors.append("[Fail!] The text of the role is NOT: APPROVER");
		}
			
		UI.NavigationMenu.TASKS.click();
		UI.Tasks.Description_textField.sendKeys(timeString);
//		UI.Tasks.Description_textField.sendKeys("20130305-11-07");
		UI.Tasks.Apply_link.click();
		Thread.sleep(sleepTime1);
		
		//Verify the status link's text is 'Cert'
		if ("Cert".equals(UI.Tasks.Status_linkOf1stResearchedResult.getText()))
				{
					logger.info("[Pass!] The text of the status link is: Cert");
				}
		else
		{
			logger.info("[Fail!] The text of the status link is NOT: Cert");
			verificationErrors.append("[Fail!] The text of the status link is NOT: Cert");
		}
		
		//Reject the task by Approver
		UI.Tasks.Status_linkOf1stResearchedResult.click();
		UI.Task.Reject_button.click();
		
		//Switch to Send Notifications of Rejection iframe.
		driver.switchTo().frame(UI.SendNotificationofRejection.wholeiframe);

		if (!"".equals(UI.SendNotificationofRejection.Send_Notification_of_Rejectionn_textMessage.getText()))
		{
			logger.info("[Pass!] The current window is: Send Notification of Rejection");
		}
		else
		{
			logger.info("[Fail!] The current window is NOT: Send Notification of Rejection");
			verificationErrors.append("[Fail!] The current window is NOT: Send Notification of Rejection");
		}
		
		
		UI.SendNotificationofRejection.Send_Button.click();
		
		//Switch back to Task Status
		driver.switchTo().defaultContent();
		
		
		//Verify the status link's text is 'Rej'
		if ("Rej".equals(UI.Tasks.Status_linkOf1stResearchedResult.getText()))
		{
			logger.info("[Pass!] The text of the status link is: Rej");
		}
		else
		{
			logger.info("[Fail!] The text of the status link is NOT: Rej");
			verificationErrors.append("[Fail!] The text of the status link is NOT: Rej");
		}
		
		
	}
	
	
	@After
	public void tearDown() throws Exception {
		CommonMethods.takeScreenShot(this.getClass(), driver);
		driver.close();
		driver.quit();
		String verificationErrorString = verificationErrors.toString();
		if (!"".equals(verificationErrorString)) {
			fail(verificationErrorString);
		}
	}
	
}
